#include "filesystem.h"

void print_help() {
    printf("\n文件系统模拟程序命令帮助:\n");
    printf("  format        - 格式化文件系统\n");
    printf("  create <name> - 创建文件\n");
    printf("  mkdir <name>  - 创建目录\n");
    printf("  open <name> <flags> - 打开文件 (0:读, 1:写, 2:读写)\n");
    printf("  close <fd>    - 关闭文件\n");
    printf("  write <fd> <content> - 写入文件\n");
    printf("  read <fd> <size> - 读取文件\n");
    printf("  delete <name> - 删除文件\n");
    printf("  ls [dir]      - 列出目录内容\n");
    printf("  cd <dir>      - 切换目录\n");
    printf("  help          - 显示帮助信息\n");
    printf("  exit          - 退出程序\n\n");
}

int main() {
    FileSystem fs;
    char command[100];
    char arg1[100];
    char arg2[100];
    char buffer[MAX_FILE_SIZE];
    
    // 初始化文件系统
    init_filesystem(&fs);
    printf("欢迎使用简单文件系统模拟程序！\n");
    printf("输入 'help' 获取命令帮助。\n\n");
    
    while (1) {
        printf("fs> ");
        fflush(stdout);
        
        // 读取命令
        if (fgets(command, sizeof(command), stdin) == NULL) {
            break;
        }
        
        // 去除换行符
        command[strcspn(command, "\n")] = '\0';
        
        // 解析命令
        int args = sscanf(command, "%s %s %s", arg1, arg2, buffer);
        
        if (strcmp(arg1, "exit") == 0) {
            printf("感谢使用文件系统模拟程序，再见！\n");
            break;
        } else if (strcmp(arg1, "help") == 0) {
            print_help();
        } else if (strcmp(arg1, "format") == 0) {
            format_filesystem(&fs);
        } else if (strcmp(arg1, "create") == 0 && args >= 2) {
            create_file(&fs, arg2);
        } else if (strcmp(arg1, "mkdir") == 0 && args >= 2) {
            create_dir(&fs, arg2);
        } else if (strcmp(arg1, "open") == 0 && args >= 3) {
            int flags = atoi(buffer);
            open_file(&fs, arg2, flags);
        } else if (strcmp(arg1, "close") == 0 && args >= 2) {
            int fd = atoi(arg2);
            close_file(&fs, fd);
        } else if (strcmp(arg1, "write") == 0 && args >= 3) {
            int fd = atoi(arg2);
            // 从buffer中提取要写入的内容
            char *content_start = strstr(command, buffer) + strlen(buffer) + 1;
            write_file(&fs, fd, content_start, strlen(content_start));
        } else if (strcmp(arg1, "read") == 0 && args >= 3) {
            int fd = atoi(arg2);
            int size = atoi(buffer);
            if (size > 0 && size <= MAX_FILE_SIZE) {
                int bytes_read = read_file(&fs, fd, buffer, size);
                if (bytes_read > 0) {
                    buffer[bytes_read] = '\0';
                    printf("读取内容: %s\n", buffer);
                }
            } else {
                printf("无效的读取大小\n");
            }
        } else if (strcmp(arg1, "delete") == 0 && args >= 2) {
            delete_file(&fs, arg2);
        } else if (strcmp(arg1, "ls") == 0) {
            if (args >= 2) {
                list_dir(&fs, arg2);
            } else {
                list_dir(&fs, ".");
            }
        } else if (strcmp(arg1, "cd") == 0 && args >= 2) {
            change_dir(&fs, arg2);
        } else {
            printf("未知命令，请输入 'help' 获取帮助。\n");
        }
    }
    
    return 0;
}